﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.PmiScale.CApPMIScaleApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.PMI.PmiScale.Resources;
using System;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.PmiScale;

public class CApPMIScaleApp
{
  private int[] m_fontSizeValues = new int[16 /*0x10*/]
  {
    8,
    9,
    10,
    11,
    12,
    14,
    16 /*0x10*/,
    18,
    20,
    22,
    24,
    26,
    28,
    36,
    48 /*0x30*/,
    72
  };
  private double m_charSizeIncrement = 0.5;
  private double m_characterSizeMinVal = 0.5;
  private double m_characterSizeMaxVal = 1000.0;
  private int m_fontSizeMinVal = 1;
  private int m_fontSizeMaxVal = 1000;

  public int[] FontSizes => this.m_fontSizeValues;

  public double CharacterSizeIncrement => this.m_charSizeIncrement;

  public double CharacterSizeMinVal => this.m_characterSizeMinVal;

  public double CharacterSizeMaxVal => this.m_characterSizeMaxVal;

  public int FontSizeMinVal => this.m_fontSizeMinVal;

  public int FontSizeMaxVal => this.m_fontSizeMaxVal;

  public void SetFontSizeForSelection(int fontSize)
  {
    bool flag1 = false;
    bool flag2 = false;
    bool flag3 = false;
    string str1 = (string) null;
    string str2 = (string) null;
    TxObjectList pmIsFromSelection = this.GetPMIsFromSelection();
    TxApplication.ActiveUndoManager.StartTransaction();
    foreach (TxPmi txPmi in (Collection<ITxObject>) pmIsFromSelection)
    {
      flag1 = true;
      if (txPmi.TextDisplayMode == 1)
      {
        flag2 = true;
        if (txPmi.HasTextPolylines)
        {
          flag3 = true;
          txPmi.FontSize = fontSize;
        }
      }
    }
    TxApplication.ActiveUndoManager.EndTransaction();
    if (!flag1)
    {
      str1 = PMIScaleStringTable.PMIS_SHOULD_BE_SELECTED;
      str2 = "TX_PMI_FONT_SIZE_NO_PMIS";
    }
    else if (!flag2)
    {
      str1 = PMIScaleStringTable.FLAT_TO_SCREEN_PMIS_SHOULD_BE_SELECTED + Environment.NewLine + PMIScaleStringTable.USE_CHARACTER_SIZE_CMD;
      str2 = "TX_PMI_FONT_SIZE_NO_FLAT_TO_SCREEN_PMIS";
    }
    else if (!flag3)
    {
      str1 = PMIScaleStringTable.PMIS_WITH_TEXT_SHOULD_BE_SELECTED;
      str2 = "TX_PMI_FONT_SIZE_NO_TEXTUAL_PMIS";
    }
    if (str1 != null)
    {
      int num = (int) TxDoNotShowAgainMessageBox.Show(str1, PMIScaleStringTable.PMI_FONT_SIZE, str2, MessageBoxButtons.OK);
    }
    TxApplication.RefreshDisplay();
  }

  public void SetCharacterSizeForSelection(double characterSize)
  {
    bool flag1 = false;
    bool flag2 = false;
    bool flag3 = false;
    string str1 = (string) null;
    string str2 = (string) null;
    TxObjectList pmIsFromSelection = this.GetPMIsFromSelection();
    TxApplication.ActiveUndoManager.StartTransaction();
    foreach (TxPmi txPmi in (Collection<ITxObject>) pmIsFromSelection)
    {
      flag1 = true;
      if (txPmi.TextDisplayMode == null)
      {
        flag2 = true;
        if (txPmi.HasTextPolylines)
        {
          flag3 = true;
          txPmi.CharacterSize = (float) characterSize;
        }
      }
    }
    TxApplication.ActiveUndoManager.EndTransaction();
    if (!flag1)
    {
      str1 = PMIScaleStringTable.PMIS_SHOULD_BE_SELECTED;
      str2 = "TX_PMI_CHARACTER_SIZE_NO_PMIS";
    }
    else if (!flag2)
    {
      str1 = $"{PMIScaleStringTable.IN_PLANE_PMIS_SHOULD_BE_SELECTED} {Environment.NewLine}{PMIScaleStringTable.USE_FONT_SIZE_CMD}";
      str2 = "TX_PMI_CHARACTER_SIZE_NO_IN_PLANE_PMIS";
    }
    else if (!flag3)
    {
      str1 = PMIScaleStringTable.PMIS_WITH_TEXT_SHOULD_BE_SELECTED;
      str2 = "TX_PMI_CHARACTER_SIZE_NO_TEXTUAL_PMIS";
    }
    if (str1 != null)
    {
      int num = (int) TxDoNotShowAgainMessageBox.Show(str1, PMIScaleStringTable.PMI_CHARACTER_SIZE, str2, MessageBoxButtons.OK);
    }
    TxApplication.RefreshDisplay();
  }

  public void GrowTextForSelection()
  {
    bool flag1 = false;
    bool flag2 = false;
    string str1 = (string) null;
    string str2 = (string) null;
    TxObjectList pmIsFromSelection = this.GetPMIsFromSelection();
    TxApplication.ActiveUndoManager.StartTransaction();
    foreach (TxPmi txPmi in (Collection<ITxObject>) pmIsFromSelection)
    {
      flag1 = true;
      if (txPmi.HasTextPolylines)
      {
        flag2 = true;
        if (txPmi.TextDisplayMode == null)
          txPmi.CharacterSize = (float) this.GetNextCharacterSize((double) txPmi.CharacterSize);
        else if (txPmi.TextDisplayMode == 1)
          txPmi.FontSize = this.GetNextFontSize(txPmi.FontSize);
      }
    }
    TxApplication.ActiveUndoManager.EndTransaction();
    if (!flag1)
    {
      str1 = PMIScaleStringTable.PMIS_SHOULD_BE_SELECTED;
      str2 = "TX_PMI_GROW_SHRINK_NO_PMIS";
    }
    else if (!flag2)
    {
      str1 = PMIScaleStringTable.PMIS_WITH_TEXT_SHOULD_BE_SELECTED;
      str2 = "TX_PMI_GROW_SHRINK_NO_TEXTUAL_PMIS";
    }
    if (str1 != null)
    {
      int num = (int) TxDoNotShowAgainMessageBox.Show(str1, PMIScaleStringTable.PMI_SCALE, str2, MessageBoxButtons.OK);
    }
    TxApplication.RefreshDisplay();
  }

  public void ShrinkTextForSelection()
  {
    bool flag1 = false;
    bool flag2 = false;
    string str1 = (string) null;
    string str2 = (string) null;
    TxObjectList pmIsFromSelection = this.GetPMIsFromSelection();
    TxApplication.ActiveUndoManager.StartTransaction();
    foreach (TxPmi txPmi in (Collection<ITxObject>) pmIsFromSelection)
    {
      flag1 = true;
      if (txPmi.HasTextPolylines)
      {
        flag2 = true;
        if (txPmi.TextDisplayMode == null)
          txPmi.CharacterSize = (float) this.GetPrevCharacterSize((double) txPmi.CharacterSize);
        else if (txPmi.TextDisplayMode == 1)
          txPmi.FontSize = this.GetPrevFontSize(txPmi.FontSize);
      }
    }
    TxApplication.ActiveUndoManager.EndTransaction();
    if (!flag1)
    {
      str1 = PMIScaleStringTable.PMIS_SHOULD_BE_SELECTED;
      str2 = "TX_PMI_GROW_SHRINK_NO_PMIS";
    }
    else if (!flag2)
    {
      str1 = PMIScaleStringTable.PMIS_WITH_TEXT_SHOULD_BE_SELECTED;
      str2 = "TX_PMI_GROW_SHRINK_NO_TEXTUAL_PMIS";
    }
    if (str1 != null)
    {
      int num = (int) TxDoNotShowAgainMessageBox.Show(str1, PMIScaleStringTable.PMI_SCALE, str2, MessageBoxButtons.OK);
    }
    TxApplication.RefreshDisplay();
  }

  public int GetFontSizeFromSelection()
  {
    int sizeFromSelection = -1;
    if (TxApplication.ActiveSelection.HasSingleSelectionInAllItems())
    {
      TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
      if (((Collection<ITxObject>) allItems).Count > 0 && ((Collection<ITxObject>) allItems)[0] is TxPmi txPmi && txPmi.TextDisplayMode == 1)
        sizeFromSelection = txPmi.FontSize;
    }
    return sizeFromSelection;
  }

  public float GetCharacterSizeFromSelection()
  {
    float sizeFromSelection = -1f;
    if (TxApplication.ActiveSelection.HasSingleSelectionInAllItems())
    {
      TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
      if (((Collection<ITxObject>) allItems).Count > 0 && ((Collection<ITxObject>) allItems)[0] is TxPmi txPmi && txPmi.TextDisplayMode == null)
        sizeFromSelection = txPmi.CharacterSize;
    }
    return sizeFromSelection;
  }

  public bool GetMinCharacterSizeFromSelection(ref float min)
  {
    min = (float) int.MaxValue;
    foreach (TxPmi txPmi in (Collection<ITxObject>) this.GetPMIsFromSelection())
    {
      if (txPmi.TextDisplayMode == null)
      {
        float characterSize = txPmi.CharacterSize;
        if ((double) characterSize < (double) min)
          min = characterSize;
      }
    }
    return (double) min < 2147483648.0;
  }

  public bool GetMaxCharacterSizeFromSelection(ref float max)
  {
    max = 0.0f;
    foreach (TxPmi txPmi in (Collection<ITxObject>) this.GetPMIsFromSelection())
    {
      if (txPmi.TextDisplayMode == null)
      {
        float characterSize = txPmi.CharacterSize;
        if ((double) characterSize > (double) max)
          max = characterSize;
      }
    }
    return (double) max > 0.0;
  }

  private TxObjectList GetPMIsFromSelection()
  {
    TxObjectList pmIsFromSelection = new TxObjectList();
    foreach (ITxObject allItem in (Collection<ITxObject>) TxApplication.ActiveSelection.GetAllItems())
    {
      if (allItem is TxPmi)
        ((Collection<ITxObject>) pmIsFromSelection).Add(allItem);
      else if (allItem is ITxObjectCollection)
      {
        foreach (TxPmi allDescendant in (Collection<ITxObject>) (allItem as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxPmi))))
        {
          if (allDescendant != null)
            ((Collection<ITxObject>) pmIsFromSelection).Add((ITxObject) allDescendant);
        }
      }
    }
    return pmIsFromSelection;
  }

  private int GetNextFontSize(int size)
  {
    int val1 = size;
    int fontSizeValue1 = this.m_fontSizeValues[0];
    int fontSizeValue2 = this.m_fontSizeValues[this.m_fontSizeValues.Length - 1];
    if (size < fontSizeValue1)
      val1 = size + 1;
    else if (size >= fontSizeValue2)
    {
      val1 = (size + 10) / 10 * 10;
    }
    else
    {
      foreach (int fontSizeValue3 in this.m_fontSizeValues)
      {
        if (fontSizeValue3 > size)
        {
          val1 = fontSizeValue3;
          break;
        }
      }
    }
    return Math.Min(val1, this.m_fontSizeMaxVal);
  }

  private int GetPrevFontSize(int size)
  {
    int val1 = size;
    int fontSizeValue1 = this.m_fontSizeValues[0];
    int fontSizeValue2 = this.m_fontSizeValues[this.m_fontSizeValues.Length - 1];
    if (size <= fontSizeValue1)
      val1 = size - 1;
    else if (size > fontSizeValue2)
    {
      val1 = Math.Max((size - 1) / 10 * 10, fontSizeValue2);
    }
    else
    {
      int num = fontSizeValue1;
      foreach (int fontSizeValue3 in this.m_fontSizeValues)
      {
        if (fontSizeValue3 >= size)
        {
          val1 = num;
          break;
        }
        num = fontSizeValue3;
      }
    }
    return Math.Max(val1, this.m_fontSizeMinVal);
  }

  private double GetNextCharacterSize(double size)
  {
    double nextCharacterSize = size;
    if (size < this.m_characterSizeMaxVal)
    {
      nextCharacterSize += this.m_charSizeIncrement * TxApplication.Options.Units.LinearMultiplier;
      if (nextCharacterSize > this.m_characterSizeMaxVal)
        nextCharacterSize = this.m_characterSizeMaxVal;
    }
    return nextCharacterSize;
  }

  private double GetPrevCharacterSize(double size)
  {
    double num = this.m_characterSizeMinVal * TxApplication.Options.Units.LinearMultiplier;
    double prevCharacterSize = size;
    if (prevCharacterSize > num)
    {
      prevCharacterSize -= this.m_charSizeIncrement * TxApplication.Options.Units.LinearMultiplier;
      if (prevCharacterSize < num)
        prevCharacterSize = num;
    }
    return prevCharacterSize;
  }
}
